#!/bin/bash

# ============================================
# 修复重复 default_server 问题
# ============================================

echo "=== 备份主配置文件 ==="
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup.$(date +%Y%m%d_%H%M%S)

echo ""
echo "=== 注释掉主配置文件中的默认 server 块 ==="
# 注释掉 default_server 的 server 块
sudo sed -i '/listen.*80 default_server/,/^    }/s/^/# /' /etc/nginx/nginx.conf
# 或者更精确地注释掉整个 server 块
sudo sed -i '/server {/,/^    }/{
    /listen.*80 default_server/{
        :a
        N
        /\n    }/!ba
        s/^/# /g
        s/\n/# /g
    }
}' /etc/nginx/nginx.conf

# 更简单的方法：直接注释掉整个 server 块
sudo sed -i '39,65s/^/# /' /etc/nginx/nginx.conf

echo ""
echo "=== 验证配置 ==="
sudo nginx -t

echo ""
echo "=== 重启 Nginx ==="
sudo systemctl restart nginx

echo ""
echo "=== 测试 ==="
sleep 1
curl -v http://127.0.0.1/api/login/ 2>&1 | head -20

echo ""
echo "✓ 修复完成！"

